package cn.itsource.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.itsource.bean.OrderAddress;
import cn.itsource.bean.OrderAdopt;
import cn.itsource.bean.Pet;
import cn.itsource.bean.UserAddress;
import cn.itsource.dto.OrderDto;
import cn.itsource.expection.BusinessException;
import cn.itsource.mapper.OrderAddressMapper;
import cn.itsource.mapper.OrderAdoptMapper;
import cn.itsource.pay.dto.PayDto;
import cn.itsource.pay.service.AlipayInfoServiceImpl;
import cn.itsource.service.IOrderAdoptService;
import cn.itsource.service.IPetService;
import cn.itsource.service.IUserAddressService;
import cn.itsource.util.MyUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-26 09:52:00
 */
@Service
public class OrderAdoptServiceImpl extends ServiceImpl<OrderAdoptMapper, OrderAdopt> implements IOrderAdoptService {

    @Resource
    private IUserAddressService userAddressService;

    @Resource
    private IPetService petService;

    @Resource
    private OrderAddressMapper orderAddressMapper;

    @Resource
    private AlipayInfoServiceImpl alipayInfoService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public String submitOrder(OrderDto orderDto) {
        Long addressId = orderDto.getAddressId();//地址id
        Long petId = orderDto.getPetId();//宠物id


        //根据用户地址id查询到用户的地址对象
        UserAddress userAddress = userAddressService.getById(addressId);
        //判空
        if (userAddress==null){
            throw new BusinessException("地址不存在");
        }

        //把对象地址拷贝到订单地址信息
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);

        //保存用户地址信息，获取id
        orderAddressMapper.insert(orderAddress);

        Long id = orderAddress.getId();

        //获取宠物信息
        Pet pet = petService.getById(petId);
        //判空
        if (pet==null) {
            throw new BusinessException("商品信息异常");
        }

        //讲订单地址的主键id设置给订单对象
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("用户地址id:"+addressId+",商品id="+petId);
        //设置支付状态
        orderAdopt.setState(0);
        //设置订单待支付金额
        orderAdopt.setPrice(BigDecimal.valueOf(pet.getCostprice()));


        //雪花算法设置订单编号
        String orderSn = new Snowflake().nextIdStr();
        orderAdopt.setOrderSn(orderSn);
        orderAdopt.setLastConfirmTime(LocalDateTime.now());
        orderAdopt.setPetId(petId);

        //获取用户id
        Long uid = MyUtils.getUid(stringRedisTemplate);
        orderAdopt.setUserId(uid);
        //设置店铺id
        orderAdopt.setShopId(pet.getShopId());

        //地址id
        orderAdopt.setAddressId(id);


        //入库订单数据
        this.save(orderAdopt);


        PayDto payDto = new PayDto();
        payDto.setMoney(pet.getCostprice());
        payDto.setSubject(pet.getName());
        payDto.setSubjectDesc("宠物订单："+orderSn);
        payDto.setOrderSn(orderSn);
        //跳转到支付
        return alipayInfoService.goBuy(payDto);
    }
}
